<template>
	<div class="orderlist">
		<van-nav-bar fixed left-text="返回" left-arrow :title="!isWeixin() ? '提交线索' : ''" @click-left="handleBack" />
		<div class="inner">
			<van-form ref="pushFormEle" label-width="100">
				<van-cell-group inset>

					<van-field name="xsimage" label="线索图片">
						<template #input>
							<van-uploader :file-list="xxImg" v-model="xxImg" :max-count="1" :limit="1"
								:after-read="afterRead_2">
								<template #preview-cover="scope">
									<div class="preview-cover van-ellipsis">
										{{ scope.name }}
									</div>
								</template>
							</van-uploader>
						</template>
					</van-field>




				</van-cell-group>

				<div style="margin: 16px">
					<van-button round block type="primary" @click="submit">
						提交线索
					</van-button>
				</div>
			</van-form>

		</div>



	</div>
</template>
<script setup lang="ts">
	import { getCookie } from "@/utils/index";
	import { ref, reactive } from "vue";
	import { useRouter, useRoute } from "vue-router";
	import { isWeixin } from "@/utils/index";
	import { fankuixsApi, uploadFileApi, userInfoApi } from "@/api/user";
	import { showLoadingToast, showSuccessToast, showFailToast, showConfirmDialog, showToast } from 'vant';
	import { json } from "stream/consumers";
	const router = useRouter();
	const route = useRoute();
	const pushFormEle = ref();
	const xxImg = ref();

	const pushForm = reactive({
		id: route.query.id,
		xsimage: '',
	});

	console.log('route.query.idroute.query.id', route.query.id)
	const handleBack = () => {
		if (history.state?.back) {
			router.back();
		} else {
			router.push("/user");
		}
	};



	// 上传线索截图
	const afterRead_2 = (file : any) => {
		let formData = new FormData();
		formData.append('file', file.file);
		console.log('这是证据图片upload返回：', formData)
		uploadFileApi(formData).then((res) => {
			if (res.code == 0) {
				pushForm.xsimage = res.data;
			} else {

			}
		}
		)
	};
	const submit = async () => {
		let userData = await userInfoApi({ uid: getCookie('token') ? Number(getCookie('token')) : '', })

		// if (!userData?.data?.phone) {
		// 	return;
		// }
		console.log('pushFormpushFormpushForm', pushForm)
		pushFormEle.value.validate().then(() => {
			let toast = showLoadingToast({
				message: '提交中...',
				loadingType: 'spinner',
				forbidClick: true,
				duration: 0
			});
			let req = {
				id: Number(pushForm.id),
				xsimage: pushForm.xsimage
			}
			fankuixsApi(req).then((res) => {
				toast.close()
				if (res.code == 0) {
					showSuccessToast({
						message: '提交成功',
						duration: 1500,
						forbidClick: true,
						onClose: () => {
							router.back()
						}
					})
				} else {
					showFailToast(res.msg)
				}
			}
			)


		})

	}
</script>
<style>
	.inner {
		margin-top: 60px;
	}
</style>